package com.BigDate.server.controller;



import com.BigDate.server.pojo.ExpComment;
import com.BigDate.server.pojo.RespBean;
import com.BigDate.server.pojo.expCommentNodeTreeVo;
import com.BigDate.server.service.IExpCommentService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author stx
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/exp-comment")
public class ExpCommentController {
    @Autowired
    private IExpCommentService iExpCommentService;

    @ApiOperation(value = "通过id获取评论")
    @GetMapping("findComment/{expId}")
    public RespBean findComment(@PathVariable Integer expId)
    {
        List<expCommentNodeTreeVo> comment = iExpCommentService.findComment(expId);
        comment.forEach(System.out::println);
        for (expCommentNodeTreeVo expCommentNodeTreeVo : comment) {
            deepSearch(expCommentNodeTreeVo);
        }
        return RespBean.success("查询成功",comment);
    }

    @ApiOperation(value = "分页获取")
    @RequestMapping(value = "/findAll/{curPage}/{pageSize}",method = RequestMethod.GET)
    public  RespBean findAll(@PathVariable Integer curPage,
                             @PathVariable Integer pageSize)
    {
        Page<expCommentNodeTreeVo> all = iExpCommentService.findAll(curPage, pageSize);
        return RespBean.success("分页查询所有",all);
    }

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public RespBean add(@RequestBody ExpComment comment)
    {
        System.out.println(comment);
        int res = iExpCommentService.add(comment);
        if (res == 1) {
            return RespBean.success("添加成功",200);
        }else{
            return RespBean.success("添加失败",404);
        }
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public RespBean delete(@PathVariable("id") Integer id)
    {
        return RespBean.success("删除成功",iExpCommentService.deleteById(id));
    }

    private void deepSearch(expCommentNodeTreeVo expCommentNodeTreeVo) {
        if (expCommentNodeTreeVo != null) {
            // 查询子评论集合
            List<expCommentNodeTreeVo> expCommentNodeTreeVos = iExpCommentService.findChildComment(expCommentNodeTreeVo.getComment_id());
            if (!CollectionUtils.isEmpty(expCommentNodeTreeVos)) {
                for (expCommentNodeTreeVo vo : expCommentNodeTreeVos) {
                    expCommentNodeTreeVo.setExpCommentNodeTreeVos(expCommentNodeTreeVos);
                    deepSearch(vo);
                }
            }
        }
    }

}
